package com.nk.iexam.ds;

import android.content.ContentValues;
import android.database.Cursor;

import com.nk.iexam.models.Category;

public class CategoryDataSource extends AbstractObjectDataSource<Category>
{
	public static final String TABLE_NAME = "tblCategory";
	public static final String SUBJECT_ID = "SubjectID";
	public static final String NAME = "Name";
	public static final String KNOWDLEDGE = "Knowdledge";
	
	public CategoryDataSource()
	{
		super();
		_tableName = TABLE_NAME;
		_allColumnsName.add(SUBJECT_ID);
		_allColumnsName.add(NAME);
		_allColumnsName.add(KNOWDLEDGE);
	}
	
	public String getTableName()
	{
		return TABLE_NAME;
	}
	
	public Category create(final long subjectId_, final String name_, final String knowdledge_) 
	{
		ContentValues values = new ContentValues();
		values.put(SUBJECT_ID, subjectId_);
		values.put(NAME, name_);
		values.put(KNOWDLEDGE, knowdledge_);
		
		long insertId = _database.insert(TABLE_NAME, null, values);
		Cursor cursor = _database.query(TABLE_NAME, getAllTableColumnsName(), ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		Category newObject = cursorToObject(cursor);
		cursor.close();
		return newObject;
	}
	
	protected Category cursorToObject(final Cursor cursor_)
	{
		Category newObject = new Category();
		newObject.setId(cursor_.getLong(0));
		newObject.setSubjectId(cursor_.getLong(1));
		newObject.setName(cursor_.getString(2));
		newObject.setKnowdledge(cursor_.getString(3));
		
		return newObject;
	}	
}
